import Mock from 'mockjs'

// 商品列表数据
const productList = Mock.mock({
  'data|8': [
    {
      'id|+1': 1,
      name: '@ctitle(8, 16)',
      price: '@float(10, 100, 2, 2)',
      points: '@integer(100, 500)',
      image: 'https://via.placeholder.com/300x300?text=Product',
      description: '@cparagraph(1)'
    }
  ]
})

// 商品详情数据
function getProductDetail(id) {
  return Mock.mock({
    id,
    name: '@ctitle(8, 16)',
    price: '@float(10, 100, 2, 2)',
    points: '@integer(100, 500)',
    description: '@cparagraph(3)',
    'images|2': ['https://via.placeholder.com/400x400?text=Detail'],
    detail: '<p>@cparagraph(5)</p><p>@cparagraph(5)</p>',
    specs: {
      品牌: '品牌@integer(1, 5)',
      类型: '类型@integer(1, 5)',
      颜色: '颜色@color',
      尺寸: '尺寸@integer(30, 50)cm'
    }
  })
}

// 模拟 API 接口
Mock.mock('/api/products', 'get', () => {
  return {
    code: 200,
    data: productList.data
  }
})

Mock.mock(/\/api\/products\/\d+/, 'get', (options) => {
  const id = parseInt(options.url.split('/').pop(), 10)
  return {
    code: 200,
    data: getProductDetail(id)
  }
})
